﻿using System;
using System.Collections.Generic;

namespace UserManagementSystem
{
    public class UserService : IUserService
    {
       
        private List<User> users;

        public UserService()
        {
            users = new List<User>();
            users.Add(new User("admin", "admin123", "admin@example.com"));
            users.Add(new User("user1", "password1", "user1@example.com"));
        }

        public bool Register(string username, string password, string email)
        {
            try
            {
                if (IsUsernameExists(username))
                {
                    Console.WriteLine("用户名已存在！");
                    return false;
                }
                
                User newUser = new User(username, password, email);
                users.Add(newUser);
                
                Console.WriteLine($"用户 {username} 注册成功！");
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"注册失败: {ex.Message}");
                return false;
            }
        }

        public bool Login(string username, string password)
        {
            foreach (User user in users)
            {
                if (user.Username == username && user.Password == password)
                {
                    Console.WriteLine($"登录成功！欢迎 {username}");
                    return true;
                }
            }
            
            Console.WriteLine("用户名或密码错误！");
            return false;
        }

        public bool IsUsernameExists(string username)
        {
            foreach (User user in users)
            {
                if (user.Username == username)
                {
                    return true;
                }
            }
            return false;
        }

        public List<User> GetAllUsers()
        {
            return new List<User>(users); 
        }

        public void ShowMainMenu()
        {
            while (true)
            {
                Console.WriteLine("\n=== 用户管理系统 ===");
                Console.WriteLine("1. 注册");
                Console.WriteLine("2. 登录");
                Console.WriteLine("3. 显示所有用户");
                Console.WriteLine("4. 退出");
                Console.Write("请选择操作: ");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        RegisterProcess();
                        break;
                    case "2":
                        LoginProcess();
                        break;
                    case "3":
                        ShowAllUsers();
                        break;
                    case "4":
                        Console.WriteLine("感谢使用！再见！");
                        return;
                    default:
                        Console.WriteLine("无效选择，请重新输入！");
                        break;
                }
            }
        }

        public void RegisterProcess()
        {
            Console.WriteLine("\n--- 用户注册 ---");
            
            Console.Write("请输入用户名: ");
            string username = Console.ReadLine();

            Console.Write("请输入密码: ");
            string password = Console.ReadLine();

            Console.Write("请输入邮箱: ");
            string email = Console.ReadLine();

            Register(username, password, email);
        }

        public void LoginProcess()
        {
            Console.WriteLine("\n--- 用户登录 ---");
            
            Console.Write("请输入用户名: ");
            string username = Console.ReadLine();

            Console.Write("请输入密码: ");
            string password = Console.ReadLine();

            Login(username, password);
        }

        private void ShowAllUsers()
        {
            Console.WriteLine("\n--- 所有注册用户 ---");
            
            if (users.Count == 0)
            {
                Console.WriteLine("暂无用户数据");
                return;
            }

            int count = 1;
            foreach (User user in users)
            {
                Console.WriteLine($"{count}. {user}");
                count++;
            }
        }
    }
}